package com.hf.db;

import java.sql.*;

public class DBProvider {
    //获取数据库连接
    public Connection getConn(){
        Connection connection = null;
        //数据库驱动器包OracleDriver的路径
        String strDriver = "oracle.jdbc.driver.OracleDriver";
        //Oracle 数据库连接串
        String strURL = "jdbc:oracle:thin:@localhost:1521:XE";
        //数据库的用户名密码
        String userName = "scott";
        String userPsw = "a123456";
        try {
            //使用DriverManger的静态方法获取数据库连接对象
            connection = DriverManager.getConnection(strURL, userName, userPsw);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return connection;
    }
    /**
     * 查询操作
     */
    public ResultSet query(String sql){
        ResultSet resultSet = null;
        try {
        //通过数据库连接对象获取查询器Statement
            Statement statement = getConn().createStatement();
            //使用查询器执行Sql语句
            resultSet = statement.executeQuery(sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return resultSet;
    }
}
